﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Xml.Linq;
using System.Windows.Input;
using System.Windows.Controls;

//A ajouter pour créer automatiquement le fichier xml
using System.Xml.Xsl;
using System.Xml;
using System.IO;

namespace MidwayBattle
{
    /// <summary>
    /// Logique d'interaction pour WindowSubscribe.xaml
    /// </summary>
    public partial class ConnectionWindow : Window
    {
        private MainWindow Mw;
        private string Lg;
        private string Pwd;
        private string ConfPwd;

        private int Objective;

        public ConnectionWindow(MainWindow Mw, int obj)
        {
            InitializeComponent();
            this.MinHeight = 300;
            this.MinWidth = 300;
            this.Mw = Mw;
            Objective = obj;
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /*
         * 
         * 
         *     
         * 
         * 
         * */



        private void NextBtn_Click(object sender, RoutedEventArgs e)
        {
            Lg = this.LoginTextBox.Text.ToUpper();
            Pwd = this.PasswordTextBox1.Password;
            ConfPwd = this.PasswordTextBox2.Password;

            try
            {
                XDocument doc = XDocument.Load("Users.xml");
                if (Lg != "Pseudo")
                {
                    IEnumerable<XElement> rech =
                             from el in doc.Root.Elements("Users")
                             where (string)el.Element("UserLog") == Lg
                             select el;

                    if (rech.Count() != 0)
                    {
                        MessageBox.Show("The login already exists", "Inscription player", MessageBoxButton.OK, MessageBoxImage.Warning);
                        this.LoginTextBox.Text = "Pseudo";
                    }
                    else
                    {
                        if (Pwd.Length < 6)
                        {
                            MessageBox.Show("New password too short or empty", "Edition player", MessageBoxButton.OK, MessageBoxImage.Warning);
                            this.PasswordTextBox1.Password = "********";
                            this.PasswordTextBox2.Password = "+-+-+-+-";
                        }
                        else
                        {
                            int i;
                            for (i = 0; i < Pwd.Length && Pwd[i] != ' '; i++) ;

                            if (i < Pwd.Length)
                            {
                                MessageBox.Show("The new password can not include spaces", "Edition player", MessageBoxButton.OK, MessageBoxImage.Warning);
                                this.PasswordTextBox1.Password = "********";
                                this.PasswordTextBox2.Password = "+-+-+-+-";
                            }
                            else
                            {
                                if (ConfPwd != Pwd)
                                {
                                    MessageBox.Show("The passwords do not correspond", "Inscription player", MessageBoxButton.OK, MessageBoxImage.Warning);
                                    this.PasswordTextBox1.Password = "********";
                                    this.PasswordTextBox2.Password = "+-+-+-+-";
                                }
                                else
                                {
                                    XElement newPlayer = new XElement(new XElement("Users", new XElement("UserLog", Lg), new XElement("UserPassword", Pwd)));
                                    doc.Root.Add(newPlayer);
                                    doc.Save("Users.xml");
                                    MessageBox.Show("The player was successfully created", "Inscription player", MessageBoxButton.OK, MessageBoxImage.Information);
                                    this.Mw.ActivMenuItems();
                                    this.Close();
                                }
                            }
                        }

                    }
                }
                else
                {
                    MessageBox.Show("The login <Pseudo> is not valid", "Inscription player", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (System.IO.FileNotFoundException ex)
            {
                if (Lg != "Pseudo")
                {
                    if (Pwd.Length < 6)
                    {
                        MessageBox.Show("New password too short or empty", "Edition player", MessageBoxButton.OK, MessageBoxImage.Warning);
                        this.PasswordTextBox1.Password = "********";
                        this.PasswordTextBox2.Password = "+-+-+-+-";
                    }
                    else
                    {
                        int i;
                        for (i = 0; i < Pwd.Length && Pwd[i] != ' '; i++) ;

                        if (i < Pwd.Length)
                        {
                            MessageBox.Show("The new password can not include spaces", "Edition player", MessageBoxButton.OK, MessageBoxImage.Warning);
                            this.PasswordTextBox1.Password = "********";
                            this.PasswordTextBox2.Password = "+-+-+-+-";
                        }
                        else
                        {
                            if (ConfPwd != Pwd)
                            {
                                MessageBox.Show("The passwords do not correspond", "Inscription player", MessageBoxButton.OK, MessageBoxImage.Warning);
                                this.PasswordTextBox1.Password = "********";
                                this.PasswordTextBox2.Password = "+-+-+-+-";
                            }
                            else
                            {
                                MessageBox.Show(ex.Message + " A new file will be created, your games will resume at zero", "Inscription player", MessageBoxButton.OK, MessageBoxImage.Error);
                                XmlTextWriter XmlTextWriter = new XmlTextWriter("Users.xml", System.Text.Encoding.UTF8);
                                XmlTextWriter.WriteStartDocument();
                                XmlTextWriter.WriteComment("Creation of new XML file"); //comment
                                XmlTextWriter.WriteStartElement("Users");
                                XmlTextWriter.WriteElementString("UserLog", Lg);
                                XmlTextWriter.WriteElementString("UserPassword", Pwd);
                                XmlTextWriter.WriteEndElement();
                                XmlTextWriter.Flush(); //empty the buffer
                                XmlTextWriter.Close(); // closes the document
                                this.Close();
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("The login <Pseudo> is not valid", "Inscription player", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }

        private void Clear_GotMouseCapture(object sender, MouseEventArgs e)
        {
            if (sender.Equals(LoginTextBox))
            {
                this.LoginTextBox.Clear();
            }
            else
            {
                if (sender.Equals(PasswordTextBox1))
                {
                    this.PasswordTextBox1.Clear();
                }
                else
                {
                    this.PasswordTextBox2.Clear();
                }
            }
        }
    }
}
